package com.quantice.mainActivity;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.quantice.Constants.StrConst;
import com.quantice.Manager.AddDialogManager;
import com.quantice.Manager.HeaderManager;
import com.quantice.Manager.TabHostManager;
import com.quantice.RowList.RowList;
import com.quantice.RowList.RowListAdapter;
import com.quantice.database.DataBaseManager;
import com.quantice.database.SQLiteHelper;

public class QuanticeActivity extends Activity {

	public static Context appContext;
	public static Resources resources;
	private TabHost th;

	public static ArrayList<RowList> rlExpenses = new ArrayList<RowList>();
	private static RowListAdapter adapterExpenses;
	public static ArrayList<RowList> rlIncomes = new ArrayList<RowList>();
	private static RowListAdapter adapterIncomes;
	private static TextView headerEx;
	private static TextView headerIn;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listview);
		appContext = getApplicationContext();
		resources = getResources();

		ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

		actionBar.setDisplayShowTitleEnabled(false);
		th = (TabHost) findViewById(R.id.th);
		TabHostManager thManager = new TabHostManager(th);
		thManager.addTab("tab1", R.id.tab1, StrConst.gastos);
		thManager.addTab("tab2", R.id.tab2, StrConst.ingresos);
		thManager.addTab("tab3", R.id.tab3, StrConst.resumen);

		DataBaseManager dbm = new DataBaseManager(this);
		rlExpenses = dbm.getAllItems(StrConst.gastos);
		rlIncomes = dbm.getAllItems(StrConst.ingresos);

		headerEx = (TextView) getLayoutInflater()
				.inflate(R.layout.header, null);
		headerIn = (TextView) getLayoutInflater()
				.inflate(R.layout.header, null);
		setUpTab(R.id.tab1);
		setUpTab(R.id.tab2);

		super.onCreate(savedInstanceState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuitem_search:
			Toast.makeText(appContext, "Search", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_add:
			addItem();
			return true;
		case R.id.menuitem_feedback:
			Toast.makeText(appContext, "Feedback", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_about:
			Toast.makeText(appContext, "About", Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menuitem_quit:
			Toast.makeText(appContext, "Quit", Toast.LENGTH_SHORT).show();
			return true;
		}
		return false;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
	}

	private void addItem() {
		final View addView = getLayoutInflater().inflate(R.layout.add, null);
		addDialog(addView);
	}

	private void addDialog(final View addView) {
		AddDialogManager addDialog = new AddDialogManager(this,
				th.getCurrentTab(), addView);
		addDialog.setPositiveButton("Aceptar",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						EditText etCant = (EditText) addView
								.findViewById(R.id.add_cant);
						EditText etfech = (EditText) addView
								.findViewById(R.id.add_fecha);
						String etCantText = etCant.getText().toString();
						String etFechText = etfech.getText().toString();
						if (etCantText.length() == 0) {
							Toast.makeText(appContext, StrConst.add_err_cant,
									Toast.LENGTH_LONG).show();
						} else {
							if (etFechText.length() == 0) {
								Toast.makeText(appContext,
										StrConst.add_error_date,
										Toast.LENGTH_LONG).show();
							} else {
								add2List(etFechText, etCantText);
							}
						}

					}
				});
		addDialog.setNegativeButton(StrConst.cancelar, null);
		addDialog.addRepeatSpinner();
		addDialog.show();
	}

	private void add2List(String date, String money) {
		money = String.valueOf(Float.parseFloat(money));
		if (money.endsWith(".0")) {
			money = money.concat("0");
		}
		if (th.getCurrentTab() == 0) {
			rlExpenses.add(new RowList(StrConst.gastos + rlExpenses.size(),
					date, money));
			DataBaseManager dbm = new DataBaseManager(this);
			dbm.insertItem(StrConst.gastos + rlExpenses.size(), date, money);
			setUpTab(R.id.tab1);
			adapterExpenses.notifyDataSetChanged();
		} else {
			rlIncomes.add(new RowList(StrConst.ingresos + rlIncomes.size(),
					date, money));
			DataBaseManager dbm = new DataBaseManager(this);
			dbm.insertItem(StrConst.ingresos + rlIncomes.size(), date, money);
			setUpTab(R.id.tab2);
			adapterIncomes.notifyDataSetChanged();
		}
		Toast.makeText(appContext, date + "-" + money + "€ is Entered",
				Toast.LENGTH_LONG).show();
	}

	private void setUpTab(int idtab) {
		ListView lvTab = (ListView) findViewById(idtab);
		if (idtab == R.id.tab1) {
			lvTab.removeHeaderView(headerEx);
			headerEx.setText(HeaderManager.getRange(rlExpenses));
			lvTab.addHeaderView(headerEx);
			adapterExpenses = new RowListAdapter(getLayoutInflater(),
					R.layout.itemlayout, rlExpenses);
			lvTab.setAdapter(adapterExpenses);
		} else if (idtab == R.id.tab2) {
			lvTab.removeHeaderView(headerIn);
			headerIn.setText(HeaderManager.getRange(rlIncomes));
			lvTab.addHeaderView(headerIn);
			adapterIncomes = new RowListAdapter(getLayoutInflater(),
					R.layout.itemlayout, rlIncomes);
			lvTab.setAdapter(adapterIncomes);
		}
	}

	public void showDatePickerDialog(View v) {
		EditText textfecha = (EditText) v.findViewById(R.id.add_fecha);
		DatePickerFragment newFragment = new DatePickerFragment(textfecha);
		newFragment.show(getFragmentManager(), "datePicker");
	}

}